import 'dart:collection';
import 'dart:io';
import 'dart:math';

import 'package:firstflutter/MyKu.dart';
import 'package:firstflutter/MyKu2.dart' as MyKu2;
import 'package:flutter/material.dart';
import 'package:path/path.dart' as path;

//import 'dart:core';

main(List<String> args) {
  print("启动参数:$args");
  print("开始执行Main函数${DateTime.now()}");

  cat = new Dog();
  print("这个结果$cat");

  const defaultPoint = const Point(0, 0);
  print("这个默认坐标:$defaultPoint");

  var point1 = new Point(2, 3);
  var point2 = new Point(3, 4);
  var jieguo = Point.distance(point1, point2);
  print("这个值$jieguo");

  var student1 = new MyKu2.Student("mikyou", 18);
  var student2 = new MyKu2.Student.withName("Chris");
  var student3 = new MyKu2.Student.withAge(24);

  print("${student1.name}_${student2.name}_${student3.name}");
  print("${student1.age}_${student2.age}_${student3.age}");

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
//    test1();

    //https://dart.dev/samples
    var result = fibonacci(20);
    print("最终结果:$result");

    print("自然常数${e}");

    print("比大小:${min(10, 20)}");

    print("我写的库调用:${Max(50, 99)}");

    print("当前系统路径:${Directory.systemTemp.path}");
    print("当前路径:${path.relative(Directory.systemTemp.path)}");

//    拆解路径
    print("拆解路径${path.split('/Users/shailen')}");
//
//    使用相对路径
    var path1 = 'docs/book.html';
    var path2 = 'articles/list';
    print(path.relative(path1, from: path2));
    print(path.relative(path2, from: path1));

//    使用绝对路径
    var samples = 'http://www.dartlang.org/samples';
    var docs = 'http://www.dartlang.org/docs';
    print(path.relative(samples, from: docs));
    print(path.relative(docs, from: samples));

//    new File('./myFile.txt').create(recursive: true).then((file) {
//      //获取完整文件名
//      print(path.basename(file.path));
//      //获取文件目录
//      print(path.dirname(file.path));
//      //获取文件名
//      print(path.basenameWithoutExtension(file.path));
//      //获取文件扩展名
//      print(path.extension(file.path));
//    });

    var clapping = '\u{1f600}';
    print(clapping);
    print(clapping.codeUnits); //返回十六位的字符单元数组
    print(clapping.runes.toList());

    Runes input = new Runes('\u2665  \u{1f605}  \u{1f60e}  \u{1f47b}  \u{1f596}  \u{1f44d}');
    print(new String.fromCharCodes(input));

    var colorList = <String>['red', 'yellow', 'blue', 'green'];
    //forEach箭头函数遍历
    colorList.forEach((color) => print(color));

    print(colorList.map((color) => '$color').join(","));

    Queue duilie = new Queue();
    duilie.add("11111");
    duilie.add("222222");
    duilie.add("3333333");
    print("队列:$duilie");

    var languages = <String>['Dart', 'Kotlin', 'Java', 'Javascript', 'Go', 'Python', 'Swift'];
    languages.forEach((language) => print('The language is $language')); //由于只有一个表达式，所以可以直接使用箭头函数。
    languages.forEach((language) {
      if (language == 'Dart' || language == 'Kotlin') {
        print('My favorite language is $language');
      }
    });

    print(languages.map((language) => 'develop language is ${language}').join('---'));

    var isDartExisted = languages.any((language) => language == 'Dart');

    print("这些语言中是否有Dart:$isDartExisted");

    var isDartAll = languages.every((language) => language == 'Dart');

    print("这数组中是否全是Dart:$isDartAll");

    var numbers = <int>[0, 3, 1, 2, 7, 4, 12, 2, 4];

    var manzu = numbers.where((int shuzi) => shuzi > 6);

    print("满足大于6的值:$manzu");

    print("${List.of(manzu).runtimeType}");

    print("如果没有找到，执行orElse代码块，可返回一个指定的默认值-1:${numbers.firstWhere((shuzi) => shuzi == 5, orElse: () => -1)}");
    print("如果没有找到，执行orElse代码块，可返回一个指定的默认值-1:${numbers.lastWhere((shuzi) => shuzi == 2, orElse: () => -1)}");
    print("如果没有找到，执行orElse代码块，可返回一个指定的默认值-1:${numbers.singleWhere((shuzi) => shuzi == 3, orElse: () => -1)}");

    var numbers1 = [3, 1, 2, 7, 12, 2, 4];
    print(/*List.of(*/ numbers1.takeWhile((number) => number > 2) /*)*/); //返回一个惰性Iterable

    print("跳过前两个元素重组一个列表:${numbers1.skip(2)}");
    print("经过函数skip后:${numbers1}");

    var languages1 = <String>['Kotlin', 'Java', 'Dart', 'Go', 'Python'];
    print("接上:${languages1.followedBy(['Swift', 'Rust', 'Ruby', 'C++', 'C#']).toList()}");
    print("原来的:${languages1}");

//    var testMap = <String,Map<String,int>>{"abc":{"niu":1}};  <String:int>
    var testList = <Map>[
      {"niu": 1},
      {"fa": 2}
    ];
    var btestList = List.of(testList.followedBy([
      {"jie": 5}
    ]));
    print("接上Jie:${btestList}");
    print("旧的:$testList");
    print("两者相等吗:${testList[0] == btestList[0]}");

    var numbers2 = <int>[3, 1, 2, 7, 12, 2, 4];
    print("累加${numbers2.reduce((prev, curr) => prev + curr)}");
    print("另一种累加${numbers2.fold(2, (prev, curr) => (prev as int) + curr)}");
    print("求平均值${numbers2.reduce((prev, curr) => prev + curr) / numbers2.length}");
    print("求平均数1${numbers.fold(2, (prev, curr) => (prev as int) + curr) / numbers.length}");
    print("累乘${numbers.reduce((prev, curr) => prev * curr)}");
    print("累乘:${numbers2.fold(2, (prev, curr) => (prev as int) * curr)}");

    var rect = new Rectangle(3, 4, 20, 15);
    rect.right = 15;
    rect.bottom = 12;

    print("矩形$rect");
    print("面积${rect.square}");
    print("除以值${97 / 6}");
    print("取整除以值${97 ~/ 6}");

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.pink,
      ),
      home: MyHomePage(title: '我的第一个flutter项目'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    print(DateTime.now());
    print("点击了一下~~~~~");
    setState(() {
      print(DateTime.now());
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
